package com.qj.yantaishan.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.IService;
import com.qj.yantaishan.domain.FinancialRecord;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 财务记录Mapper接口
 *
 * @author qihe
 * @date 2022-07-22
 */
public interface FinancialRecordMapper extends BaseMapper<FinancialRecord> {
    /**
     * 查询财务记录
     *
     * @param pigcmsId 财务记录主键
     * @return 财务记录
     */
    public FinancialRecord selectFinancialRecordByPigcmsId(Long pigcmsId);

    /**
     * 查询财务记录列表
     *
     * @param financialRecord 财务记录
     * @return 财务记录集合
     */
    public List<FinancialRecord> selectFinancialRecordList(FinancialRecord financialRecord);

    /**
     * 新增财务记录
     *
     * @param financialRecord 财务记录
     * @return 结果
     */
    public int insertFinancialRecord(FinancialRecord financialRecord);

    /**
     * 修改财务记录
     *
     * @param financialRecord 财务记录
     * @return 结果
     */
    public int updateFinancialRecord(FinancialRecord financialRecord);

    /**
     * 删除财务记录
     *
     * @param pigcmsId 财务记录主键
     * @return 结果
     */
    public int deleteFinancialRecordByPigcmsId(Long pigcmsId);

    /**
     * 批量删除财务记录
     *
     * @param pigcmsIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteFinancialRecordByPigcmsIds(String[] pigcmsIds);

    @Select("select name,logo,store_pay_income,deficit from shanglala_store")
    Map<String, Object> selectStoreMap();

    @Select("select sum(if(income<0,0,income)) from shanglala_financial_record")
    BigDecimal selectTotalGetMoney();

    @Select("select sum(income)*-1 from shanglala_financial_record sfr left join shanglala_order so on sfr.order_id = so.order_id where sfr.income<0 and sfr.type=3  and is_agent=1  \n")
    BigDecimal selectTotalReturnMoney();

    @Select(" select unbalance from shanglala_store")
    BigDecimal selectTotalBalance();

    @Select("select sum(sfr.income) from shanglala_financial_record sfr left join shanglala_order so on sfr.order_id = so.order_id where sfr.type in(1,6) and so.is_agent=1 and so.status in(2,3)\n" +
            "   ")
    BigDecimal selectTotalIncome();

    @Select("select ifnull(sum(amount),0) from shanglala_store_withdrawal where type in (1,2) and add_time >#{startTime} and add_time < #{endTime}")
    BigDecimal selectWithdrawByTime(@Param("startTime") Long startTime, @Param("endTime") Long endTime);

    @Select("select ifnull(sum(income)*-1 ,0)from shanglala_financial_record sfr left join shanglala_order so on sfr.order_id = so.order_id where sfr.income<0 and sfr.type=3  and is_agent=1 " +
            " and sfr.add_time>#{startTime} and sfr.add_time < #{endTime}")
    BigDecimal selectReturnByTime(@Param("startTime") Long startTime, @Param("endTime") Long endTime);

    @Select("select  count(*) \n" +
            "\n" +
            "from  shanglala_financial_record  sfr inner join shanglala_order so on sfr.order_id = so.order_id  ")
    int selectIncomeTotal();

    @Select("select sfr.add_time,sfr.pigcms_id id,sfr.order_no,sfr.trade_no,sfr.income,so.status order_status \n" +
            "\n" +
            "from  shanglala_financial_record  sfr inner join shanglala_order so on sfr.order_id = so.order_id  limit #{current},#{pageSize}")
    List<Map<String, Object>> selectIncomeList(@Param("current") Integer current, @Param("pageSize") Integer pageSize);

    @Select("  select sum(income) from shanglala_financial_record where order_no =  #{orderNo}")
    BigDecimal selectIncomeByOrderNo(Object orderNo);
    @Select("select count(*) from shanglala_financial_record where income<0   ")

    int selectInoutdetailTotal();

    @Select("select add_time,type,income, trade_no from shanglala_financial_record where income<0 limit #{current},#{pageSize}   ")
    List<Map<String, Object>> selectInoutdetailList(@Param("current") Integer current, @Param("pageSize") Integer pageSize);
    @Select("select ssw.pigcms_id id , ssw.withdrawal_type ,ssw.trade_no,ssw.sales_ratio,ssw.amount,(ssw.amount-(ssw.amount*(ssw.sales_ratio/100)))  actual,ssw.complate_time,ssw.status ,ssw.name ,ssw.alipay_number ,su.nickname,su.phone from shanglala_store_withdrawal  ssw left join shanglala_user su on ssw.uid = su.uid limit #{current},#{pageSize}")
    List<Map<String, Object>> selectWithdrawList(@Param("current") Integer current, @Param("pageSize") Integer pageSize);
    @Select("select count(*) from shanglala_store_withdrawal  ssw left join shanglala_user su on ssw.uid = su.uid ")

    int selectWithdrawTotal();
}
